using AvalonAdmin.BaseViewModel;
using AvalonAdmin.Consts;
using AvalonAdmin.Helpers;
using AvalonAdminDto.CarType.CarTypeDtos;
using AvalonAdminDto.CarType.VehicleSeriesDtos;
using AvalonAdminDto.Common;
using D.UtilCore.Dtos;
using System;
using System.Threading.Tasks;

namespace AvalonAdmin.Pages.CarType.ViewModels.CarType
{
    /// <summary>
    /// 车型修改视图模型
    /// </summary>
    public class CarTypeModifyViewModel : BaseModifyViewModel
    {
        #region 页面属性及初始化数据

        private string? _id;
        
        public CarTypeModifyViewModel(string? id, ViewStatus? viewStatus) : base()
        {
            _id = id;
            SetupFormStatus(viewStatus);
            if (!string.IsNullOrEmpty(id))
            {
                _ = Task.Run(async () => await QueryAsync());
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        public override async Task<bool> QueryAsync()
        {
            if (string.IsNullOrEmpty(_id)) return false;

            try
            {
                var res = await HttpHelper<RepCarType>.GetAsync($"{ApiUrl.GetCarType}/{_id}");
                if (res.Code == StatusCodeEnum.OK && res.Data != null)
                {
                    CurrentCarType = res.Data;

                    // 加载车系信息
                    //if (!string.IsNullOrEmpty(CurrentCarType.VehicleSeriesId))
                    //{
                    //    var vehicleSeriesRes = await HttpHelper<RepVehicleSeries>.GetAsync($"{ApiUrl.GetVehicleSeries}/{CurrentCarType.VehicleSeriesId}");
                    //    if (vehicleSeriesRes.IsSuccess)
                    //    {
                    //        _repVehicleSeries = vehicleSeriesRes.Data;
                    //        OnPropertyChanged(nameof(VehicleSeries));
                    //    }
                    //}
                }
                else if (res.Code == StatusCodeEnum.Waring)
                {
                    ShowWarning(res.Msg);
                }
                else
                {
                    ShowError(res.Msg ?? "加载车型数据失败");
                }
            }
            catch (System.Exception ex)
            {
                ShowError($"加载车型数据失败：{ex.Message}");
            }

            return true;
        }
        #endregion

        #region 数据编辑属性

        private RepCarType _currentCarType;
        private RepVehicleSeries? _repVehicleSeries;

        /// <summary>
        /// 当前车型
        /// </summary>
        public RepCarType CurrentCarType
        {
            get => _currentCarType;
            set
            {
                _currentCarType = value;
                OnPropertyChanged();
            }
        }
       
        /// <summary>
        /// 车型名称
        /// </summary>
        public string? Name
        {
            get => CurrentCarType?.Name;
            set
            {
                if (CurrentCarType != null)
                {
                    CurrentCarType.Name = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 英文名称
        /// </summary>
        public string? EnglishName
        {
            get => CurrentCarType?.EnglishName;
            set
            {
                if (CurrentCarType != null)
                {
                    CurrentCarType.EnglishName = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 车系
        /// </summary>
        public string? VehicleSeries => _repVehicleSeries?.Name;
        #endregion

        #region 提交，重置

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <returns></returns>
        protected override async Task SubmitAsync()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Name))
                {
                    await ShowMsgWarning("车型名称不能为空");
                    return;
                }

                var carType = new RepCarType
                {
                    Id = _id,
                    Name = Name,
                    EnglishName = EnglishName,
                    VehicleSeriesId = CurrentCarType.VehicleSeriesId
                };

                ApiResult<int> res;
                if (string.IsNullOrEmpty(_id))
                {
                    res = await HttpHelper<int>.PostAsync(ApiUrl.AddCarType, carType);
                    HandleApiResponse(res, "新增成功", "新增失败");
                }
                else
                {
                    res = await HttpHelper<int>.PutAsync(ApiUrl.UpdateCarType, carType);
                    HandleApiResponse(res, "修改成功", "修改失败");
                }
            }
            catch (Exception ex)
            {
                await ShowMsgError($"操作失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 重置表单
        /// </summary>
        protected override void Reset()
        {
            CurrentCarType = new RepCarType();
            _repVehicleSeries = null;
            OnPropertyChanged(nameof(Name));
            OnPropertyChanged(nameof(EnglishName));
            OnPropertyChanged(nameof(VehicleSeries));
        }

        #endregion
    }
}